package com.example.module_home.View.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.animation.LayoutAnimationController;
import android.widget.Toast;

import com.blankj.utilcode.util.FragmentUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.example.module_base.Base.BaseFragment;
import com.example.module_home.Adapter.AllOperationAdapter;
import com.example.module_home.Entity.UserOperationInfo;
import com.example.module_home.Module.CodeBank.CodeBankManager;
import com.example.module_home.Public.Constant;
import com.example.module_home.R;
import com.example.module_home.Utils.DialogUtil;
import com.example.module_home.View.HomeCodeActivity;
import com.example.module_home.databinding.HomeFragmentAlloperationBinding;
import com.timmy.tdialog.TDialog;

import java.util.List;

/**
 * Created by Administrator on 2018/4/25 0025.
 */

public class HMOperationFragment extends BaseFragment<HomeFragmentAlloperationBinding> {
    private static final String TAG = "HMOperationFragment";
    private HomeFragmentAlloperationBinding mBinding;
    private List<UserOperationInfo> mInfoList;
    private AllOperationAdapter mAllOperationAdapter;
    private boolean isInitLoad = true;//是否是初始化加载

    @Override
    public int setlayoutid() {
        return R.layout.home_fragment_alloperation;
    }

    @Override
    public void init(HomeFragmentAlloperationBinding binding) {
        this.mBinding = binding;
        setupRefreshOperation();
        loadAllOperations(DialogUtil.showLoadingDialog(mContext));
    }

    /**
     * 设置下拉刷新配置
     */
    private void setupRefreshOperation() {
        mBinding.refreshOperation.mPtrClassicHeader.setRefreshIcon(mContext.getDrawable(R.drawable.ic_refresh));
        mBinding.refreshOperation.mPtrClassicHeader.setRefreshTextColor(mContext.getResources().getColor(R.color.darkslateblue));
        mBinding.refreshOperation.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                loadAllOperations(null);
            }
        });
    }

    /**
     * 初始化加载最近操作信息
     */
    private void loadAllOperations(@Nullable TDialog waitDialog) {
        CodeBankManager.getInstance(mContext).getAllOperations(waitDialog, new CodeBankManager.CodeBankBack<List<UserOperationInfo>>() {
            @Override
            public void successful(List<UserOperationInfo> infoList) {
                mInfoList = infoList;
                if(isInitLoad){
                    LinearLayoutManager manager = new LinearLayoutManager(mContext);
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    mAllOperationAdapter = new AllOperationAdapter(infoList);
                    DividerItemDecoration decoration = new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL);
                    decoration.setDrawable(getResources().getDrawable(R.drawable.bg_line_item));
                    mBinding.recyAlloperation.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL));
                    mBinding.recyAlloperation.setLayoutManager(manager);
                    isInitLoad = false;
                }else {
                    mBinding.refreshOperation.refreshComplete();
                    mAllOperationAdapter.setNewData(infoList);
                }
                mBinding.recyAlloperation.setAdapter(mAllOperationAdapter);
                ViewingCode(mAllOperationAdapter);
            }

            @Override
            public void failed(int code, String e) {
                if(!isInitLoad){
                    mBinding.refreshOperation.refreshComplete();
                }
            }
        });
    }

    private void ViewingCode(AllOperationAdapter adapter) {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                HMCodeWebFragment codeWebFragment = new HMCodeWebFragment();
//                Bundle bundle = new Bundle();
//                bundle.putInt(Constant.CODE_ID,mInfoList.get(position).getKindID());
//                codeWebFragment.setArguments(bundle);
//                FragmentUtils.add(getFragmentManager(),codeWebFragment,R.id.con_container,false,true);
                Intent intent = new Intent(mContext, HomeCodeActivity.class);
                intent.putExtra(Constant.CODE_ID,mInfoList.get(position).getKindID());
                startActivity(intent);
            }
        });
    }
}
